<template>
  <!-- 份库存表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商编码" prop="supplierCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.supplierCode"
              placeholder="请选择供应商"
              clearable
            >
              <template #append>
                <el-button type="success" plain @click="showWorkOrderSelect2"
                  >选择</el-button
                >
              </template>
            </el-input>
            <span v-else>{{ formData.supplierCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次" prop="stockBatch">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.stockBatch"
              placeholder="请输入批次"
              clearable
            />

            <span v-else>{{ formData.stockBatch }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流水号" prop="serial">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.serial"
              placeholder="请输入流水号"
              clearable
            />
            <span v-else>{{ formData.serial }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
    <npm-partner-select
      :visible="selectDialogSELECT2.visible"
      @change-visible="changeVisibleSELECT2"
      :brekList="brekList2"
    />
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "NPMWMSPARTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  stockBatch: [{ required: true, message: "请填写批次号", trigger: "blur" }],
  supplierCode: [{ required: true, message: "请选择供应商", trigger: "blur" }],
  serial: [{ required: true, message: "请填写流水号", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  serial: "",
  id: "",
  partCode: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  stockBatch: "",
  supplyBatch: "",
  supplierId: "",
  supplierCode: "",
  supplierName: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  warehouseId: "",
  warehouseCode: "",
  warehouseName: "",
  storageTypeId: "",
  storageTypeCode: "",
  storageTypeName: "",
  storageSectionId: "",
  storageSectionCode: "",
  binId: "",
  binCode: "",
  storageUnitId: "",
  extenalCode: "",
  goodsReceiptTime: "",
  materialDocumentId: "",
  materialDocumentCode: "",
  materialDocumentDetailId: "",
  materialDocumentDetailCode: "",
  totalQuantity: "",
  availableQuantity: "",
  freezeQuantity: "",
  transferQuantity: "",
  quantityUnit: "",
  usedColume: "",
  usedArea: "",
  usedWeight: "",
  isCheck: "",
  chemicalCompositionId: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
  storageSectionName: "",
  shelfId: "",
  shelfCode: "",
  shelfName: "",
  binName: "",
  storageCode: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        partCode,
        materialId,
        materialCode,
        materialName,
        stockBatch,
        supplyBatch,
        supplierId,
        supplierCode,
        supplierName,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        warehouseId,
        warehouseCode,
        warehouseName,
        storageTypeId,
        storageTypeCode,
        storageTypeName,
        storageSectionId,
        storageSectionCode,
        binId,
        binCode,
        storageUnitId,
        extenalCode,
        goodsReceiptTime,
        materialDocumentId,
        materialDocumentCode,
        materialDocumentDetailId,
        materialDocumentDetailCode,
        totalQuantity,
        availableQuantity,
        freezeQuantity,
        transferQuantity,
        quantityUnit,
        usedColume,
        usedArea,
        usedWeight,
        isCheck,
        chemicalCompositionId,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        storageSectionName,
        shelfId,
        shelfCode,
        shelfName,
        binName,
        storageCode,
      } = newVal;
      Object.assign(formData, {
        id,
        partCode,
        materialId,
        materialCode,
        materialName,
        stockBatch,
        supplyBatch,
        supplierId,
        supplierCode,
        supplierName,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        warehouseId,
        warehouseCode,
        warehouseName,
        storageTypeId,
        storageTypeCode,
        storageTypeName,
        storageSectionId,
        storageSectionCode,
        binId,
        binCode,
        storageUnitId,
        extenalCode,
        goodsReceiptTime,
        materialDocumentId,
        materialDocumentCode,
        materialDocumentDetailId,
        materialDocumentDetailCode,
        totalQuantity,
        availableQuantity,
        freezeQuantity,
        transferQuantity,
        quantityUnit,
        usedColume,
        usedArea,
        usedWeight,
        isCheck,
        chemicalCompositionId,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        storageSectionName,
        shelfId,
        shelfCode,
        shelfName,
        binName,
        storageCode,
      });
      formData.serial = formData.storageCode.substring(13, 19);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.partCode = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.stockBatch = "";
  formData.supplyBatch = "";
  formData.supplierId = "";
  formData.supplierCode = "";
  formData.supplierName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.warehouseId = "";
  formData.warehouseCode = "";
  formData.warehouseName = "";
  formData.storageTypeId = "";
  formData.storageTypeCode = "";
  formData.storageTypeName = "";
  formData.storageSectionId = "";
  formData.storageSectionCode = "";
  formData.binId = "";
  formData.binCode = "";
  formData.storageUnitId = "";
  formData.extenalCode = "";
  formData.goodsReceiptTime = "";
  formData.materialDocumentId = "";
  formData.materialDocumentCode = "";
  formData.materialDocumentDetailId = "";
  formData.materialDocumentDetailCode = "";
  formData.totalQuantity = "";
  formData.availableQuantity = "";
  formData.freezeQuantity = "";
  formData.transferQuantity = "";
  formData.quantityUnit = "";
  formData.usedColume = "";
  formData.usedArea = "";
  formData.usedWeight = "";
  formData.isCheck = "";
  formData.chemicalCompositionId = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
  formData.storageSectionName = "";
  formData.shelfId = "";
  formData.shelfCode = "";
  formData.shelfName = "";
  formData.binName = "";
  formData.storageCode = "";
}

const loading = ref(false);

/** 份库存保存提交 */
function handleSubmit() {
  if (formData.supplierCode.length != 6) {
    ElMessage.error("供应商编号必须为6位");
    return;
  }
  if (formData.stockBatch.length != 6) {
    ElMessage.error("批次必须为6位");
    return;
  }
  if (formData.serial.length != 6) {
    ElMessage.error("流水号必须为6位");
    return;
  }
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/wms/wmspart/saveEdit",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
const selectDialogSELECT2 = ref({
  visible: false,
});
const changeVisibleSELECT2 = (val: boolean, data: any) => {
  selectDialogSELECT2.value.visible = val;
  if (data) {
    console.log("选择的数据", data);
    formData.supplierId = data.partnerId;
    formData.supplierName = data.partnerName;
    formData.supplierCode = data.partnerCode;
  }
};

const brekList2 = ref([]);
const showWorkOrderSelect2 = () => {
  console.log("showWorkOrderSelect");
  selectDialogSELECT2.value.visible = true;
  // if (formData.mesWorkOrderPlanId != "") {
  //   brekList2.value.push(formData);
  // }
};
</script>
